package studentmanager;

import gui.GUIMain;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import structure.Course;
import structure.Student;
import stuff.FixedSizeDocument;

public class GUIStudentEdit extends JDialog{
	private static final long serialVersionUID = 1L;
	private JTextField tfMSTS;
	private JTextField tfFirstName;
	private JTextField tfLastName;
	private JTextField tfDateOfBirth;
	private JTextField tfIDNumber;
	private JTextField tfUser;
	private JTextField tfPass;
	private JButton btAdd;
	private JButton btCancel;
	private Course course;
	private Student currentStudent;
	
	public GUIStudentEdit(JFrame parent, Student stu, Course course){
		super(parent,"Chỉnh sửa thí sinh", true);
		this.currentStudent = stu;
		this.course = course;

		init();
		putDefaultValues();
		setLayout(new BorderLayout());
		
		JPanel centerLayout = new JPanel();
		centerLayout.setBorder(new EmptyBorder(10, 10, 10, 10));
		centerLayout.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx=0;
		c.gridy=1;
		c.weightx = 0.2f;
		centerLayout.add(new JLabel("Họ tên :"),c);
		c.gridx=1;
		c.weightx = 1f;
		centerLayout.add(tfFirstName,c);
		c.gridx=2;
		c.weightx = 1f;
		centerLayout.add(tfLastName,c);
		
		c.gridx=0;
		c.gridy=0;
		c.weightx = 0f;
		centerLayout.add(new JLabel("MSTS :"),c);
		c.gridx=1;
		c.gridwidth = 2;
		centerLayout.add(tfMSTS,c);
		
		c.gridx=0;
		c.gridy=2;
		c.weightx = 0f;
		centerLayout.add(new JLabel("Ngày sinh :"),c);
		c.gridx=1;
		c.gridwidth = 2;
		centerLayout.add(tfDateOfBirth,c);
		
		c.gridx=0;
		c.gridy=3;
		c.gridwidth = 1;
		centerLayout.add(new JLabel("CMND :"),c);
		c.gridx=1;
		c.gridwidth = 2;
		centerLayout.add(tfIDNumber,c);
		
		c.gridx=0;
		c.gridy=4;
		c.gridwidth = 1;
		centerLayout.add(new JLabel("Tên Đ.nhập :"),c);
		c.gridx=1;
		c.gridwidth = 2;
		centerLayout.add(tfUser,c);
		
		c.gridx=0;
		c.gridy=5;
		c.gridwidth = 1;
		centerLayout.add(new JLabel("Mật khẩu :"),c);
		c.gridx=1;
		c.gridwidth = 2;
		centerLayout.add(tfPass,c);
		
		add(centerLayout,BorderLayout.CENTER);
		
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(btAdd);
		buttonPanel.add(btCancel);
		
		add(buttonPanel,BorderLayout.SOUTH);
	}
	
	private void init(){
		tfMSTS = new JTextField();
		tfMSTS.setDocument(new FixedSizeDocument(10));
		tfFirstName = new JTextField();
		tfFirstName.setDocument(new FixedSizeDocument(50));
		tfLastName = new JTextField();
		tfLastName.setDocument(new FixedSizeDocument(50));
		tfDateOfBirth = new JTextField();
		tfDateOfBirth.setDocument(new FixedSizeDocument(10));
		tfIDNumber = new JTextField();
		tfIDNumber.setDocument(new FixedSizeDocument(9));
		tfUser = new JTextField();
		tfUser.setDocument(new FixedSizeDocument(20));
		tfPass = new JTextField();
		tfPass.setDocument(new FixedSizeDocument(20));
		
		btAdd = new  JButton(new BtAddAction());
		btAdd.setText("Sửa");
		btCancel = new JButton(new btCancelAction());
		btCancel.setText("Thoát");
	}
	
	private void putDefaultValues(){
		this.tfMSTS.setText(currentStudent.getMSTS());
		this.tfFirstName.setText(currentStudent.getFirstName());
		this.tfLastName.setText(currentStudent.getLastName());
		this.tfDateOfBirth.setText(currentStudent.getDateOfBirth());
		this.tfIDNumber.setText(currentStudent.getCMND());
		this.tfUser.setText(currentStudent.getUsername());
		this.tfPass.setText(currentStudent.getPassword());
	}
	
	class BtAddAction extends AbstractAction{

		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(tfMSTS.getText().trim().equals("")){
				JOptionPane.showMessageDialog(GUIStudentEdit.this, "Mã số thí sinh không được để trống",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(tfFirstName.getText().trim().equals("")){
				JOptionPane.showMessageDialog(GUIStudentEdit.this, "Mã số thí sinh không được để trống",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(tfLastName.getText().trim().equals("")){
				JOptionPane.showMessageDialog(GUIStudentEdit.this, "Mã số thí sinh không được để trống",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(tfDateOfBirth.getText().trim().equals("")){
				JOptionPane.showMessageDialog(GUIStudentEdit.this, "Mã số thí sinh không được để trống",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(tfIDNumber.getText().trim().equals("")){
				JOptionPane.showMessageDialog(GUIStudentEdit.this, "Mã số thí sinh không được để trống",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(tfUser.getText().trim().equals("")){
				JOptionPane.showMessageDialog(GUIStudentEdit.this, "Mã số thí sinh không được để trống",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(tfPass.getText().trim().equals("")){
				JOptionPane.showMessageDialog(GUIStudentEdit.this, "Mã số thí sinh không được để trống",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
				return;
			}
			
			double mark;
			try {
				mark = GUIMain.getDb().getMarkofStudent(currentStudent.getiD());
			} catch (SQLException e) {
				e.printStackTrace();
				return;
			}
			
			boolean rs = GUIMain.getDb().UpdateStudent(currentStudent.getiD(),tfMSTS.getText().trim(), 
					tfFirstName.getText().trim(), tfLastName.getText().trim(), 
					tfDateOfBirth.getText().trim(), tfIDNumber.getText().trim(), 
					course.getCourseID(),mark, tfUser.getText().trim(), tfPass.getText().trim());
			
			if(rs){
				JOptionPane.showMessageDialog(GUIStudentEdit.this, "Cập nhật thông tin thí sinh " + 
			tfFirstName.getText().trim() + " " + tfLastName.getText().trim() + " thành công.",
						"Thông báo", JOptionPane.INFORMATION_MESSAGE);
			}else{
				JOptionPane.showMessageDialog(GUIStudentEdit.this, "Cập nhật thí sinh thất bại",
						"Thông báo", JOptionPane.ERROR_MESSAGE);
			}
			dispose();
		}
	}
	
	class btCancelAction extends AbstractAction{

		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent e) {
			dispose();
		}
	}
}
